package com.qf.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qf.cache.TDirtyWordCache;
import com.qf.constans.ResultCode;
import com.qf.entity.TAdminUser;
import com.qf.entity.TDirtyWord;
import com.qf.events.DirtyWordEvent;
import com.qf.events.UpdateBlackEvent;
import com.qf.exceptions.MyBaseException;
import com.qf.mapper.DirtyWordMapper;
import com.qf.service.DirtyWordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import java.util.List;
import java.util.stream.Collectors;

/*
杨威
2021/10/26
8:58
*/
@Service
@Transactional
public class DirtyWordServiceImpl implements DirtyWordService {
    private DirtyWordMapper dirtyWordMapper;
    private TDirtyWordCache dirtyWordCache;
    private ApplicationContext context;
    @Autowired
    public void setDirtyWordCache(TDirtyWordCache dirtyWordCache) {
        this.dirtyWordCache = dirtyWordCache;
    }
    @Autowired
    public void setContext(ApplicationContext context) {
        this.context = context;
    }

    @Autowired
    public void setDertyWordMapper(DirtyWordMapper dirtyWordMapper){
        this.dirtyWordMapper=dirtyWordMapper;
    }
    @Override
    @Transactional(readOnly = true, propagation = Propagation.SUPPORTS)
    //查看所有 要查看当前用户创建的信息 别人创建的不能查看 管理员可以查看所有 所以需要一个session
    public PageInfo<TDirtyWord> selectAll(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        //从缓存中获取数据
        List<TDirtyWord> dirtyWordList = dirtyWordCache.getAllData();

        //获取session
        RequestAttributes requestAttributes = RequestContextHolder.currentRequestAttributes();
        Object user = ((ServletRequestAttributes) requestAttributes).getRequest().getSession().getAttribute("user");
        //对集合进行操作 拉姆达表达式
        List<TDirtyWord> dirtyWords = dirtyWordList.stream().filter(word -> {
            //返回的是全局或者创建者的数据 不返回别人的数据 除非是管理员
            return word.getOwntype() == 1 || word.getCreater() == ((TAdminUser) user).getId() || ((TAdminUser) user).getType() == 1;
        }).collect(Collectors.toList());
        //这是按照用户能看到的数据进行分页操作
        List subList=dirtyWords.stream().skip((pageNum-1)*pageSize).limit(pageSize).collect(Collectors.toList());
        PageInfo<TDirtyWord> pageInfo=new PageInfo(subList);
        //设置总条数
        pageInfo.setTotal(dirtyWords.size());
        return pageInfo;
    }

    @Override
    public TDirtyWord showById(Integer id) {
        if(id<=0|| ObjectUtils.isEmpty(id)){
            throw new  MyBaseException("传递的id不合法", ResultCode.FAIL);
        }
        return dirtyWordMapper.selectById(id);
    }

    @Override
    public Integer add(TDirtyWord td) {
        if(td.getId()<0||td.getOwntype()<=0||td.getCreater()<=0||ObjectUtils.isEmpty(td.getOwntype())||ObjectUtils.isEmpty(td.getCreater())){
            throw new MyBaseException("主键或者owntype或者creater不合法",ResultCode.FAIL);
        }
        Integer insert = dirtyWordMapper.insert(td);
        context.publishEvent(new DirtyWordEvent());
        return insert;
    }

    @Override
    public Integer modify(TDirtyWord td) {
        if(td.getId()<=0||td.getOwntype()<=0||td.getCreater()<=0||ObjectUtils.isEmpty(td.getId())||ObjectUtils.isEmpty(td.getOwntype())||ObjectUtils.isEmpty(td.getCreater())){
            throw new MyBaseException("主键或者owntype或者creater不合法",ResultCode.FAIL);
        }
        Integer update = dirtyWordMapper.update(td);
        context.publishEvent(new DirtyWordEvent());
        return update;
    }

}
